package com.huixue.cn.ztej.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TSchoolInfo;
import com.huixue.cn.ztej.bean.TSubjectInfo;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.TSubjectService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;

@Domain(TSubjectInfo.class)
@Controller
@RequestMapping(value = "/sys/tsubject")
public class TSubjectController {

	@Autowired(required = true)
	private TSubjectService tSubjectService;
	
	@RequestMapping(value = "/list")
	public String list(Model model,HttpServletRequest request) {
	Map<String,Object> searchParams = new HashMap<String,Object>();
		Integer pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
		Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
		Results results = tSubjectService.searchByPage(searchParams,pageNumber,pageSize);
		model.addAttribute("res", results);
		return "/tsubject/list";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/tsubject/add";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate/{id}")
	public String toUpate(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TSubjectInfo info = tSubjectService.getInfo(id);
		model.addAttribute("roleRes", info);
		return "/tsubject/update";
	}
	
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/udpate",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results udpate(@RequestBody TSubjectInfo info,Model model,HttpServletRequest request) {
		tSubjectService.update(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/add",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results add(@RequestBody TSubjectInfo info,HttpServletRequest request) {
		tSubjectService.save(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	@RequestMapping(value = "listall", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String listAll(HttpServletRequest request,HttpServletResponse response) {
		Map<String, Object> resulMmap = new HashMap<String, Object>();
		List<TSubjectInfo> list = tSubjectService.findAll();
		resulMmap.put("list", list);
		resulMmap.put("result", "success");
		return JSON.toJSONString(resulMmap);
	}
}
